/*
 * Copyright (C) 2007-2008 SlideMe client
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.slideme.android.manager.download;

import android.content.Context;
import org.slideme.android.manager.ContentHandler;
import org.slideme.android.manager.UnsupportedContentHandlerException;
import org.slideme.android.manager.download.impl.AndroidContentHandler;
import org.slideme.android.manager.download.impl.ExternalInstallerContentHandler;
import org.slideme.android.manager.download.impl.ImageContentHandler;

/**
 * Provides services for downloading of content.
 */
public interface DownloadContext {

    /**
     * Returns the download user agent appropriate for this context.
     *
     * @return the download user agent appropriate for this context.
     */
    DownloadUserAgent getDownloadUserAgent();

    /**
     * Returns the content handler for the specified mime-type.
     *
     * @param mimetype the mime-type for the returned content handler.
     * @return the content handler for the specified mime-type.
     * @throws UnsupportedContentHandlerException
     *          if content handler for the specified mime-type could not be found.
     */
    ContentHandler getContentHandlerFor(String mimetype) throws UnsupportedContentHandlerException;

    /**
     * Returns the android content context.
     *
     * @return the android content context
     */
    Context getContext();

    /**
     * Initializes download context
     *
     * @param context the android content context
     */
    void init(Context context);

    public static class Factory {
        /**
         * Constructor
         */
        private Factory() {
        }

        /**
         * Returns a default instance of the download context.
         *
         * @return a default instance of the  download context
         */
        public static DownloadContext createAndroidDownloadContext(final String mimetype) {
            return new DownloadContext() {
                private Context context;

                public DownloadUserAgent getDownloadUserAgent() {
                    DownloadUserAgent agent = DownloadUserAgent.Factory.createDefaultDownloadUserAgent(mimetype);
                    agent.init(this);
                    return agent;
                }

                public ContentHandler getContentHandlerFor(String mimetype) throws UnsupportedContentHandlerException {
                    if (mimetype == null) {
                        throw new IllegalArgumentException("mimetype");
                    }
                    ContentHandler handler;
                    if (mimetype.equals("application/x-android")) {
                        handler = new AndroidContentHandler();

                    } else if (mimetype.equals("image/*")) {
                        handler = new ImageContentHandler();
                    } else if (mimetype.equals("application/x-android-extern")) {
                        handler = new ExternalInstallerContentHandler();
                    } else {
                        throw new UnsupportedContentHandlerException();
                    }

                    handler.init(context);
                    return handler;
                }

                public Context getContext() {
                    return context;
                }

                public void init(Context context) {
                    if (context == null) {
                        throw new IllegalArgumentException("context");
                    }
                    this.context = context;
                }
            };
        }
    }
}
